#include "portal_md5.h"

void portal_dec_des_ecb(unsigned char *data, unsigned char *encbuf, unsigned long bufsize)
{
    DES_key_schedule ks;
    DES_cblock kk;
    uchar *src, *dst;
    unsigned long j, len;

    memset(&kk, 0, sizeof(kk));
    memset(&ks, 0, sizeof(ks));
    strncpy((char *)&kk, portal_args.key, 8);
    DES_set_key(&kk, &ks);

    src = data;
    dst = encbuf;
    len = 0;
    for (j = 0; j < bufsize; j += 8) {
        DES_ecb_encrypt((const_DES_cblock *)src, (DES_cblock *)dst, &ks, DES_DECRYPT);
        src += 8;
        dst += 8;
        len += 8;
    }
}

void portal_dec_des_ncbc(uchar *data, uchar *encbuf, ulong bufsize)
{
    DES_key_schedule ks;
    DES_cblock kk;
    uchar iv[8];

    memset(&kk, 0, sizeof(kk));
    memset(&ks, 0, sizeof(ks));
    memset(&iv, 0, sizeof(iv));
    DES_string_to_key(portal_args.key, &kk);
    DES_set_key_unchecked(&kk,&ks);

    DES_ncbc_encrypt(data,
                     encbuf,(long)bufsize,
                     &ks,
                     (DES_cblock *)iv,
                     DES_DECRYPT);
}

void portal_md5(void *input, long length, char *digest)
{
    MD5_CTX context;

    if (input == NULL || digest == NULL) {
        return;
    }

    MD5_Init(&context);
    MD5_Update(&context, input, length);
    MD5_Final((uchar *)digest, &context);
}

